<?php
/**
 * Created by PhpStorm.
 * User: D
 * Date: 2019/5/14
 * Time: 17:15
 */

header("Access-Control-Allow-Origin:*");
header("Content-Type:Application/json;charset=utf-8");

include "dbHelper.php";

if ($_SERVER["REQUEST_METHOD"] == "GET") {
    print_r(json_encode(Array("msg" => "不支持get请求", "status" => -1)));
    return;
}
//支持post请求

if (isset($_REQUEST["u_id"]) &&
    isset($_REQUEST["pro_id"]) &&
    isset($_REQUEST["pro_name"]) &&
    isset($_REQUEST["pro_price"]) &&
    isset($_REQUEST["pro_img"]) &&
    isset($_REQUEST["pro_num"])) {
    //1.要判断是否已经购买过,

    $queryPro = "SELECT * FROM `cart` WHERE c_pro_id='" . $_REQUEST["pro_id"] . "' AND c_u_id='" . $_REQUEST["u_id"] . "'";
    $result = dbHelper($queryPro);
    if ($result->num_rows >= 1) {
        $Total = $_REQUEST["pro_price"] * $_REQUEST["pro_num"];
        //如果已经购买,只需要修改数量和金额
        $updateSQL = "UPDATE cart  SET c_pro_num=c_pro_num+1,c_pro_total='" . $Total . "' WHERE c_u_id='" . $_REQUEST["u_id"] . "'  AND c_pro_id='" . $_REQUEST["pro_id"] . "'";
        $updateStatus = dbHelper($updateSQL);
        if ($updateStatus) {
            print_r(json_encode(Array("msg" => "购物车加入成功u", "status" => 1)));
        } else {
            print_r(json_encode(Array("msg" => "购物车加入失败u", "status" => 0)));
        }
    } else {
        //如果没购买 把产品的信息插入到购物车.
        $Total = $_REQUEST["pro_price"] * $_REQUEST["pro_num"];

        $insertSQL = "INSERT INTO cart (`c_pro_id`,`c_pro_name`,`c_pro_price`,`c_pro_num`,`c_pro_total`,`c_pro_img`,`c_u_id`) VALUES ('" . $_REQUEST["pro_id"] . "','" . $_REQUEST["pro_name"] . "','" . $_REQUEST["pro_price"] . "','" . $_REQUEST["pro_num"] . "','" . $Total . "','" . $_REQUEST["pro_img"] . "','" . $_REQUEST["u_id"] . "')";
        $inserStatus = dbHelper($insertSQL);
        if ($inserStatus) {
            print_r(json_encode(Array("msg" => "购物车加入成功I", "status" => 1)));
        } else {
            print_r(json_encode(Array("msg" => "购物车加入失败I", "status" => 0)));
        }
    }
} else {
    print_r(json_encode(Array("msg" => "参数不完整", "status" => -11)));
}
//1.用户id,谁购买

//2.产品名称,
//3 产品价格
//4 产品数量
//5.产品总金额 =产品价格*  产品数量
//6 产品的图片

//header("Content-Type:application/json;charset=utf-8");
//header("Access-Control-Allow-Origin:*");
//include "dbHelper.php";
//
//if($_SERVER["REQUEST_METHOD"]=="GET"){
//    print_r(json_encode(Array("msg"=>"不支持get请求","status"=>-1)));
//    return;
//}
////支持post请求
//if (isset($_REQUEST["u_id"])&&
//    isset($_REQUEST["pro_id"])&&
//    isset($_REQUEST["pro_name"])&&
//    isset($_REQUEST["pro_price"])&&
//    isset($_REQUEST["pro_img"])&&
//    isset($_REQUEST["pro_num"])){
//
//    $queryPro="select * from `carts` WHERE c_pro_id='".$_REQUEST["u_id"]."' AND c_u_id='".$_REQUEST["u_id"]."'";
//
//    $result=dbHelper($queryPro);
//
//    if($result->num_rows>=1){//已经购买 如果已经加入只需要修改产品数量和金额
//        $Total=$_REQUEST["pro_price"] * $_REQUEST["pro_num"];
//
//        $updateSQL="UPDATE carts SET c_pro_num='".$_REQUEST["pro_num"]."',c_pro_total='".$Total."' WHERE c_u_id='".$_REQUEST["u_id"]."' AND c_pro_id='".$_REQUEST["pro_id"]."'";
//
//        $updateStatus=dbHelper($updateSQL);
//        if($updateStatus){
//            print_r(json_encode(Array("mag"=>"购物车加入成功a","status"=>1)));
//        }else{
//            print_r(json_encode(Array("mag"=>"购物车加入失败a","status"=>0)));
//        }
//
//    }else{//如果没加入 把产品信息插入购物车
//        //总价
//        $Total=$_REQUEST["pro_price"] * $_REQUEST["pro_num"];
//
//        $insertSQL="INSERT INTO `carts`(`c_pro_id`,`c_pro_name`,`c_pro_price`,`c_pro_num`,`c_pro_total`,`c_pro_img`,`c_u_id`)
//VALUES ('".$_REQUEST["pro_id"]."','".$_REQUEST["pro_name"]."','".$_REQUEST["pro_price"]."','".$_REQUEST["pro_num"]."','".$Total."','".$_REQUEST["pro_img"]."','".$_REQUEST["u_id"]."')";
//
//        $insertStatus=dbHelper($insertSQL);
//
//        if($insertStatus){
//            print_r(json_encode(Array("msg"=>"购物车加入成功b","status"=>1)));
//        }else{
//            print_r(json_encode(Array("msg"=>"购物车加入失败b","status"=>0)));
//        }
//    }
//
//
//}else{
//    print_r(json_encode(Array("msg"=>"参数不完整","status"=>-11)));
//}
